namespace WfWeb.Common
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	using MethodIT.Helper;

	/// <summary>
	///		Summary description for YearMonth.
	/// </summary>
	public partial class YearMonth : System.Web.UI.UserControl
	{



		private static readonly int START_YEAR	= 2004;
		private static readonly int END_YEAR	= DateTime.Now.Year;
		private static readonly int MONTH		= DateTime.Now.Month;

		private string _yearMonth = string.Empty;


		protected void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				this.GetYearDropDownList(ddlYear);
				this.GetMonthDropDownList(ddlMonth);
				this.SetValue(_yearMonth);
			}
		}

		#region private methods
		private void GetYearDropDownList(DropDownList ddl)
		{
			for(int tempInt=START_YEAR;tempInt<=END_YEAR;tempInt++)
			{
				AddItem(ddl,tempInt.ToString(),tempInt.ToString());
			}
			ddl.SelectedValue = END_YEAR.ToString();
		}

		private void GetMonthDropDownList(DropDownList ddl)
		{
			for(int tempInt=1;tempInt<=12;tempInt++)
			{
				if(tempInt<10) AddItem(ddl,"0"+tempInt.ToString(),"0"+tempInt.ToString());
				else AddItem(ddl,tempInt.ToString(),tempInt.ToString());
			}
			if(MONTH<10) ddl.SelectedValue = "0"+MONTH.ToString();
			else ddl.SelectedValue = MONTH.ToString();
		}

		private static void AddItem(DropDownList ddl,string text,string newvalue) 
		{
			ListItem listItem =new ListItem();
			listItem.Text = text;
			listItem.Value= newvalue;
			ddl.Items.Add(listItem);
		}
		#endregion

		#region public methods
		public string GetYear()
		{
			return ddlYear.SelectedValue;
		}

		public string GetMonth()
		{
			return ddlMonth.SelectedValue;
		}

//		public void HideLabels()
//		{
//			this.labelYear.Visible	= false;
//			this.labelMonth.Visible = false;
//		}

		public void SetValue(int value)
		{
			this._yearMonth = value.ToString();
		}

		private void SetValue(string yearmonth)
		{
			if(yearmonth.Length == 6)
			{
				ddlYear.SelectedValue	= yearmonth.Substring(0,4);
				ddlMonth.SelectedValue	= yearmonth.Substring(4,2);
			}
		}

		public int GetValue()
		{
			return HelperDigit.ConvertToInt32(ddlYear.SelectedValue+ddlMonth.SelectedValue);
		}
		#endregion

		#region controls
		public DropDownList YearDropDownList
		{
			get 
			{ 
				return this.ddlYear;
			}
		}
		public DropDownList MonthDropDownList
		{
			get 
			{ 
				return this.ddlMonth;
			}
		}
		#endregion

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{

		}
		#endregion
	}
}
